﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionePolveri2.DAL;
using GestionePolveriLib.BL.Reports;
using FrameworkLib.Facades;

namespace GestionePolveriLib.Facades.Reports
{
    public class Report524Facade : FacadeBase<GestionePolveriEntities>
    {
        Filter524 m_filter = null;

        public Report524Facade()
        {

        }

        public Report524Facade(Filter524 filter)
        {
            m_filter = filter;
        }

        public List<Report524> GetReport524Data()
        {
            List<Report524> retValue = null;

            var obj = from x in GetContext().Lotti
                      where ((x.DataDdt >= m_filter.PeriodFrom) && (x.DataDdt <= m_filter.PeriodTo))
                      select new Report524
                      {
                          CodicePolvere = x.AnagPolveri.CodicePolvere,
                          Quantita = x.Giacenza,
                          ScortaMinima = x.AnagPolveri.ScortaMinima
                      };

            if (obj.Any())
            {
                retValue = obj.ToList();

                foreach (Report524 r in retValue)
                {
                    r.ColoriAssociati = this.GetColoriAssociati(r.CodicePolvere);
                }
            }

            return retValue;
        }

        public string GetColoriAssociati(string codicePolvere)
        {
            string retValue = null;

            try
            {
                GestionePolveriEntities context = this.GetContext();
                var obj = (from x in context.ComposizioneColori
                           where x.AnagPolveri.CodicePolvere == codicePolvere
                           select (x.AnagColori.CodiceColore + ", ")).Distinct();

                if (obj.Any())
                {
                    obj.ToList().ForEach(x => retValue += x);

                    int i = retValue.LastIndexOf(",");

                    retValue = retValue.Substring(0, i);
                }

            }
            catch (Exception ex)
            {
            }

            return retValue;
        }
    }
}
